package com.yxp.offer;

/**
 * @author : yang.xp
 * @date : 2021/9/2 9:02
 * @description : 剑指 Offer 15. 二进制中1的个数
 * <p>
 * 每个数的二进制都比其小1的整数的二进制少1
 *
 * 10 1010 1001  1000  ret = 1
 * 8  1000 0111  0000  ret = 2
 *
 */
public class Problem15 {

    public static void main(String[] args) {
        Problem15 problem15 = new Problem15();
        System.out.println(problem15.hammingWeight(10));
    }

    public int hammingWeight(int n) {
        int ret = 0;
        while (n != 0) {
            n &= n - 1;
            ret++;
        }
        return ret;
    }
}
